<?php

Pfw_Loader::loadClass('Pfw_Controller_Route_PostCondition');

class Prj_Controller_Route_FormatCondition implements Pfw_Controller_Route_PostCondition {
    public function exec($route_name, &$route_result) {
        if (preg_match('/\.json$/', $route_result['action'])) {
            // this will rewrite all actions of the form <action>.json to route to 
            // a method <action>Json in your controller:
            $route_result['action'] = preg_replace('/\.json$/', '-json', $route_result['action']);
            
            // alternatively, you could rewrite the <action>.json to <action> and set 
            // another $route_result parameter, for example 'format' to indicate the requested
            // response format. you can then get the response format by calling 
            // $this->getParam('format') in your controller.
            $count = 0;
            $route_result['action'] = preg_replace('/\.json$/', '', $route_result['action'], 1, $count);
            if ($count > 0) {
                $route_result['format'] = 'json';
            }
        }
    }
}